<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/20
 * Time: 16:16
 */

namespace app\api\controller;


use app\api\model\UserAddress;
use app\common\controller\ReturnCode;
use think\Controller;
use think\Request;

class Address extends Controller
{
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this -> openId = $request -> param('openid');
        $this -> addressId = $request -> param('addressid');
    }
    //收货地点列表
    public function index(){
        if(empty($this -> openId)){
            return json(['code' => 2, 'msg'=> ReturnCode::$return_code[2]]);
        }
        $where = ['openid' => $this -> openId];
        $res = UserAddress::where($where)->select();
        if($res){
            return json(['code' => 0 ,'msg' => ReturnCode::$return_code[0],'data'=>$res]);
        }
        return json(['code' => 10, 'msg' => ReturnCode::$return_code[10]]);
    }
    //添加
    public function addressadd(Request $request){
        $sex = $request -> param('sex',1);
        $name = $request -> param('name');
        $phone = $request -> param('phone');
        $address = $request -> param('address');
        $lon = $request -> param('lon',0);
        $lat = $request -> param('lat',0);
        if(empty($this -> openId) || empty($name) || empty($address) || !check_mobile($phone)){
            return json(['code' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        $data = [
            'openid' => $this -> openId ,
            'u_sex' => $sex,
            'u_name' => $name,
            'u_phone' => $phone,
            'address_name'=> $address,
            'lon' => $lon,
            'lat' => $lat,
        ];
        $has = UserAddress::where($data)->find();
        if($has){
            return json(['code' => 23,'msg' => ReturnCode::$return_code[23]]);
        }
        $res = UserAddress::create($data) -> allowField(true);
        if($res){
            return json(['status' => 0 ,'msg' => ReturnCode::$return_code[0]]);
        }
        return json(['status' => 10,'msg' => ReturnCode::$return_code[10]]);

    }
    //详情
    public function addressinfo(){
        if(empty($this -> openId) || empty($this -> addressId)){
            return json(['code' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        $where = ['address_id' => $this -> addressId];
        $res = UserAddress::where($where) -> find();
        if($res){
            return json(['status' => 0 ,'msg' => ReturnCode::$return_code[0],'data' => $res]);
        }
        return json(['status' => 10,'msg' => ReturnCode::$return_code[10]]);

    }
    //修改
    public function addressup(Request $request){
        $sex = $request -> param('sex',1);
        $name = $request -> param('name');
        $phone = $request -> param('phone');
        $address = $request -> param('address');
        $lon = $request -> param('lon',0);
        $lat = $request -> param('lat',0);
        if(empty($this -> openId) || empty($name) || empty($address) || !check_mobile($phone) || empty($this -> addressId)){
            return json(['code' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        $data = [
            'openid' => $this -> openId ,
            'u_sex' => $sex,
            'u_name' => $name,
            'u_phone' => $phone,
            'address_name'=> $address,
            'lon' => $lon,
            'lat' => $lat,
        ];
        $has = UserAddress::where($data)->find();
        if($has){
            return json(['code' => 23,'msg' => ReturnCode::$return_code[23]]);
        }
        $where = ['address_id' => $this -> addressId];
        $res = UserAddress::update($data,$where)->allowField(true);
        if($res){
            return json(['status' => 0 ,'msg' => ReturnCode::$return_code[0]]);
        }
        return json(['status' => 5,'msg' => ReturnCode::$return_code[5]]);
    }
    //删除
    public function addressdel(){
        if(empty($this -> openId) || empty($this -> addressId)){
            return json(['code' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        $where = ['address_id' => $this -> addressId];
        $has = UserAddress::where($where)->find();
        if(!$has){
            return json(['code'=> 10,'msg'=>ReturnCode::$return_code[10]]);
        }
        $res = UserAddress::destroy($where);
        if($res){
            return json(['status' => 0 ,'msg' => ReturnCode::$return_code[0]]);
        }
        return json(['status' => 5,'msg' => ReturnCode::$return_code[5]]);
    }
}